package com.xonro.common.histrans.sfeg.biz.fund;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.CommTransBiz;
import com.xonro.common.histrans.sfeg.biz.common.SfegRecordTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class BaseFundBiz extends SfegRecordTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        SDK.getLogAPI().consoleInfo("打印获取的建工资产信息数据========"+mainData);

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_MDM_ASSET");//项目基础信息
        logModel.setOuterTable("tb_zcSBInfos");//基础项目信息表
        logModel.setOuterPK(mainData.getString("GSID"));
        logModel.setOuterExt1(mainData.getString("outFactoryNumber"));
        logModel.setOuterExt2(mainData.getString("type63"));//资产大类
        logModel.setOuterExt3(mainData.getString("type64"));//资产小类
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("ASSET_NO",mainData.getString( "GSID"));//资产编号
        mainBO.set("FACTORY_NUMBER", mainData.getString("outFactoryNumber"));//出厂编号
        mainBO.set("PROJECT_NO", mainData.getString("xmbh"));//项目编号
        mainBO.set("STORAGE_USERID", CommTransBiz.getInstance().getUserId(  mainData.getString("fzr")));//负责人编号
        mainBO.set("ASSET_NAME", mainData.getString("sbName"));//设备名称
        mainBO.set("UNIT", mainData.getString("danwei"));//计量单位
        mainBO.set("MODEL", mainData.getString("xxgg"));//规格、型号
        mainBO.set("MODEL", mainData.getString("xxgg"));//规格、型号

        mainBO.set("LEVEL1_NO", mainData.getString("type63"));//大类编号
        mainBO.set("LEVEL2_NO", mainData.getString("type64"));//小类编号
        mainBO.set("ORIGINAL_VALUE", mainData.getString("svalue"));//物品原值
        mainBO.set("ASSET_STATUS", mainData.getString("status"));//状态
        if(StringUtils.isNotBlank( mainData.getString("gmDate"))) {
            mainBO.set("PURCHASE_DATE", DateUtil.parse(mainData.getString("gmDate"), "yyyy-MM-dd"));//购买日期
        }
        mainBO.set("FACTORY_NAME", mainData.getString("sccj"));//生产厂家
        mainBO.set("DIMENSION", mainData.getString("wxcc"));//外形尺寸

        mainBO.set("ASSET_WEIGHT", mainData.getString("wpjz"));//物品净重

        mainBO.set("WARRANTY_PERIOD", mainData.getString("bxqx"));//保修期限

        mainBO.set("FEATURE", mainData.getString("jstx"));//技术特性

        mainBO.set("ZJYears", mainData.getString("DEPRECIATION_PERIOD"));//折旧期限

        mainBO.set("ASSET_ATTRIBUTE", mainData.getString("zcType"));//资产属性
        mainBO.set("ACCEPTANCE_DATE",  DateUtil.parse(mainData.getString("ysDate"),"yyyy-MM-dd"));//验收日期


        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);
        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //印章id
        fileTransData.setFkCode(mainData.getString("id"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_MDM_ASSET");
        fileTransData.setBoName("BO_XR_MDM_ASSET");
        fileTransData.setFieldName("ATTACHMENT");//附件
    }

    @Override
    protected String getProcessDefId() {
        return "obj_4ac9b0793034491688aeba41838f8b6c";
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }

    @Override
    protected String getActivityDefId() {
        return null;
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String gsid = ctx.getMainData().getString("GSID");//资产编号
        String outid = ctx.getMainData().getString("outFactoryNumber");//出厂编号

        return gsid+outid;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        String gsid = ctx.getMainData().getString("GSID");//资产编号
        return gsid;
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_zcSBInfos";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        String gsid = historyTransContext.getMainData().getString("GSID");//资产编号
        return gsid;
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_zcSBInfos WHERE orcode='1040' ");
    }
}
